<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible = true">增加停车计费规则</el-button>
      <el-button @click="exportToExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ payType(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="showDialog(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 弹框 -->
    <el-dialog :visible="dialogVisible" width="590px" title="新增规则" @close="resetForm">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="top">
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" style="width: 390px;" placeholder="请输入计费规则编号" size="mini" />
          </el-form-item>
          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" style="width: 390px;" placeholder="请输入计费规则名称" size="mini" />
          </el-form-item>
          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>
              <el-radio label="turn" border>按次收费</el-radio>
              <el-radio label="partition" border>分段收费</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="flex-container">
            <el-form-item label="免费时长">
              <el-input
                v-model="addForm.freeDuration"
                style="width: 160px; margin-right: 5px;"
                placeholder="请输入免费时长"
                size="mini"
              />分钟
            </el-form-item>
            <el-form-item label="收费上限">
              <el-input
                v-model="addForm.chargeCeiling"
                style="width: 160px; margin-right: 5px;"
                placeholder="请输入收费上限"
                size="mini"
              />元
            </el-form-item>
          </div>
          <el-form-item label="计费规则">
            <!-- 按时长收费区域 -->
            <div v-show="addForm.chargeType === 'duration'" class="duration">
              每 <el-input
                v-model="addForm.durationTime"
                style="width: 105px;"
                size="small"
                placeholder="请输入"
                class="input-box"
              /> 小时
              <el-input
                v-model="addForm.durationPrice"
                style="width: 105px;"
                size="small"
                placeholder="请输入"
                class="input-box"
              /> 元
            </div>
            <!-- 按次收费区域 -->
            <div v-show="addForm.chargeType === 'turn'" class="turn">
              每次 <el-input v-model="addForm.turnPrice" style="width:  105px;" size="small" class="input-box" /> 元
            </div>
            <!-- 按分段收费区域 -->
            <div v-show="addForm.chargeType === 'partition'" class="partition">
              <div class="item"><el-input
                v-model="addForm.partitionFrameTime"
                style="width: 105px;"
                size="small"
                class="input-box"
              />小时内,每小时收费<el-input
                v-model="addForm.partitionFramePrice"
                style="width:105px;"
                size="small"
                class="input-box"
              /> 元
              </div>
              <div class="item">每增加<el-input
                v-model="addForm.partitionIncreaseTime"
                style="width:105px;"
                size="small"
                class="input-box"
              />小时,增加<el-input
                v-model="addForm.partitionIncreasePrice"
                style="width:105px;"
                size="small"
                class="input-box"
              /> 元
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="resetForm">取 消</el-button>
        <el-button size="mini" type="primary" @click="changePay">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRuleList, addRule, getRuleInfo, updateRule, deleteRule } from '@/api/pay/pay'
import { utils, writeFileXLSX } from 'xlsx'
export default {
  name: 'Building',
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false, // 控制弹框关闭打开
      addForm: {
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱

      }, // 计费规则表单对象
      addFormRules: {
        ruleNumber: [
          {
            required: true,
            message: '请输入规则编号',
            trigger: 'blur'
          }
        ],
        ruleName: [
          {
            required: true,
            message: '请输入规则名称',
            trigger: 'blur'
          }
        ],
        chargeType: [
          {
            required: true,
            message: '请选择收费类型',
            trigger: 'blur'
          }
        ]
      } // 计费规则校验规则对象
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    async getRuleList() {
      const data = await getRuleList(this.params)
      this.ruleList = data.rows
      this.total = data.total
    },
    // 付费方式枚举
    payType(type) {
      switch (type) {
        case 'duration':
          return '按时长收费'
        case 'turn':
          return '按次收费'
        default:
          return '分段收费'
      }
    },
    // 导出excel
    async exportToExcel() {
      // 获取要导出的业务数据
      const res = await getRuleList(this.params)
      // 表头英文字段key
      const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // 表头中文字段value
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 以excel表格的顺序调整后端数据
      const sheetData = res.rows.map((item) => {
        const obj = {}
        tableHeaderKeys.forEach(key => {
          obj[key] = item[key]
        })
        return obj
      })
      // 创建一个工作表
      const worksheet = utils.json_to_sheet(sheetData)
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 把工作表添加到工作簿
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    },
    // 添加规则
    async showDialog(id) {
      this.dialogVisible = true
      const data = await getRuleInfo(id)
      this.addForm = data
    },
    // 修改规则 / 新增规则
    async changePay() {
      await this.$refs.addForm.validate()
      if (this.addForm.id) {
        await updateRule(this.addForm)
        this.$message.success('修改成功')
      } else {
        await addRule(this.addForm)
        this.$message.success('添加成功')
      }
      this.getRuleList()
      this.resetForm()
    },

    // 删除
    async del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteRule(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRuleList()
        })
    },
    // 关闭弹框 重置表单数据
    resetForm() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
      this.addForm = {
        ruleNumber: '', // 计费规则编号
        ruleName: '', // 计费规则名称
        chargeType: 'duration', // 计费规则类型 duration / turn / partition
        chargeCeiling: null,
        freeDuration: null,
        // 时长计费独有字段
        durationTime: null, // 时长计费单位时间
        durationPrice: null, // 时长计费单位价格
        durationType: 'hour',
        // 按次收费独有字段
        turnPrice: null,
        // 分段计费独有字段
        partitionFrameTime: null, // 段内时间
        partitionFramePrice: null, // 段内费用
        partitionIncreaseTime: null, // 超出时间
        partitionIncreasePrice: null // 超出费为收费价钱
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
  .el-form{
  width: 390px;
  .flex-container {
    display: flex;
    justify-content: space-between;
  }
  .el-form-item {
    display: flex;
    flex-direction: column;

    .el-form-item__content {

      display: flex;
      justify-content: space-between;

      .el-radio-group{
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 16px;
      }

      .duration{
        display: flex;
        justify-content: space-between;
      }
      }

      .el-form--label-top {
        .el-form-item__label {
            padding: 0;
          }
      }
  }
}
}

</style>
